package com.jingbit.ai.helper;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.io.StreamCorruptedException;

/**
 * 数据持久化
 * 
 * @author FreemanLee
 * 
 */
public class PersistentHelper {

	private final String TAG = "PersistentHelper";
	private static PersistentHelper instance = null;

	private PersistentHelper() {
	}

	public static PersistentHelper getInstance() {
		if (instance == null) {
			instance = new PersistentHelper();
		}
		return instance;
	}

	/**
	 * 读取本地对象
	 * 
	 * @return
	 */
	public Serializable readObj(String fileName) {
		if (StringHelper.hasValue(fileName)) {
			return null;
		}
		Serializable obj = null;
		ObjectInputStream in = null;
		try {
			String path = FileHelper.getDiskCachePath("persistents")+"/"+fileName;
			File file = new File(path);
			if (!file.exists()) {
				return null;
			}
			in = new ObjectInputStream(new FileInputStream(path));
			obj = (Serializable) in.readObject();
			in.close();
		} catch (StreamCorruptedException e) {
			Logger.log(TAG, "StreamCorruptedException");
		} catch (FileNotFoundException e) {
			Logger.log(TAG, "FileNotFoundException");
		} catch (IOException e) {
			Logger.log(TAG, "IOException");
		} catch (ClassNotFoundException e) {
			Logger.log(TAG, "ClassNotFoundException");
		} catch (NullPointerException e) {
			Logger.log(TAG, "NullPointerException");
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					Logger.log(TAG, e.getMessage());
				} finally {
					in = null;
				}
			}
		}
		return obj;
	}
	
	/**
	 * 读取本地对象
	 * 
	 *            文件路径
	 * @return
	 */
	public Serializable readObj(String folderName,String fileName) {
		if (StringHelper.hasValue(fileName)) {
			return null;
		}
		Serializable obj = null;
		ObjectInputStream in = null;
		try {
			String path = FileHelper.getDiskCachePath("persistents")+"/"+folderName;
			File file = new File(path);
			if (!file.exists()) {
				return null;
			}
			path = path+"/"+fileName;
			in = new ObjectInputStream(new FileInputStream(path));
			obj = (Serializable) in.readObject();
			in.close();
		} catch (StreamCorruptedException e) {
			Logger.log(TAG, "StreamCorruptedException");
		} catch (FileNotFoundException e) {
			Logger.log(TAG, "FileNotFoundException");
		} catch (IOException e) {
			Logger.log(TAG, "IOException");
		} catch (ClassNotFoundException e) {
			Logger.log(TAG, "ClassNotFoundException");
		} catch (NullPointerException e) {
			Logger.log(TAG, "NullPointerException");
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					Logger.log(TAG, e.getMessage());
				} finally {
					in = null;
				}
			}
		}
		return obj;
	}
	

	/**
	 * 持久化对象
	 * 
	 * @param obj
	 * @param fileName
	 *            文件路径
	 * @return
	 */
	public boolean saveObj(Serializable obj, String fileName) {
		if (StringHelper.hasValue(fileName)) {
			return false;
		}
		ObjectOutputStream out = null;
		try {
			String path = FileHelper.getDiskCachePath("files")+"/"+fileName;
			out = new ObjectOutputStream(new FileOutputStream(path));
			out.writeObject(obj);
			out.close();
			out = null;
			return true;
		} catch (FileNotFoundException e) {
			Logger.log(TAG, e.getMessage());
		} catch (IOException e) {
			Logger.log(TAG, e.getMessage());
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					Logger.log(TAG, e.getMessage());
				} finally {
					out = null;
				}
			}
		}
		return false;
	}
	
	/**
	 * 持久化对象
	 * 
	 * @param obj
	 * @param fileName
	 *            文件路径
	 * @return
	 */
	public boolean saveObj(Serializable obj,String folderName,String fileName) {
		if (StringHelper.hasValue(fileName)) {
			return false;
		}
		ObjectOutputStream out = null;
		try {
			String path = FileHelper.getDiskCachePath("files")+"/"+folderName;
			File f = new File(path);
			if (!f.exists()) {
				f.mkdirs();
			}
			path = path+"/"+fileName;
			out = new ObjectOutputStream(new FileOutputStream(path));
			out.writeObject(obj);
			out.close();
			out = null;
			return true;
		} catch (FileNotFoundException e) {
			Logger.log(TAG, e.getMessage());
		} catch (IOException e) {
			Logger.log(TAG, e.getMessage());
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					Logger.log(TAG, e.getMessage());
				} finally {
					out = null;
				}
			}
		}
		return false;
	}

}
